package org.jeecg.modules.basicGoods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basicGoods.entity.BasicGoods;
import org.jeecg.modules.basicGoods.service.IBasicGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 货物名称信息
 * @Author: jeecg-boot
 * @Date:   2024-08-14
 * @Version: V1.0
 */
@Api(tags="货物名称信息")
@RestController
@RequestMapping("/basicGoods/basicGoods")
@Slf4j
public class BasicGoodsController extends JeecgController<BasicGoods, IBasicGoodsService> {
	@Autowired
	private IBasicGoodsService basicGoodsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param basicGoods
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "货物名称信息-分页列表查询")
	@ApiOperation(value="货物名称信息-分页列表查询", notes="货物名称信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BasicGoods basicGoods,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BasicGoods> queryWrapper = QueryGenerator.initQueryWrapper(basicGoods, req.getParameterMap());
		Page<BasicGoods> page = new Page<BasicGoods>(pageNo, pageSize);
		IPage<BasicGoods> pageList = basicGoodsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/topology")
	public Result<?> topology() {
		LambdaQueryWrapper<BasicGoods> wrapper = Wrappers.lambdaQuery();
		wrapper.orderByDesc(BasicGoods::getPriority);
		List<BasicGoods> list = basicGoodsService.list(wrapper);
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param basicGoods
	 * @return
	 */
	@AutoLog(value = "货物名称信息-添加")
	@ApiOperation(value="货物名称信息-添加", notes="货物名称信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BasicGoods basicGoods) {
		basicGoodsService.save(basicGoods);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param basicGoods
	 * @return
	 */
	@AutoLog(value = "货物名称信息-编辑")
	@ApiOperation(value="货物名称信息-编辑", notes="货物名称信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BasicGoods basicGoods) {
		basicGoodsService.updateById(basicGoods);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "货物名称信息-通过id删除")
	@ApiOperation(value="货物名称信息-通过id删除", notes="货物名称信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		basicGoodsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "货物名称信息-批量删除")
	@ApiOperation(value="货物名称信息-批量删除", notes="货物名称信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.basicGoodsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "货物名称信息-通过id查询")
	@ApiOperation(value="货物名称信息-通过id查询", notes="货物名称信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BasicGoods basicGoods = basicGoodsService.getById(id);
		if(basicGoods==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(basicGoods);
	}

	@AutoLog(value = "货物名称信息-根据引领型和提送货查询货物名称列表")
	@ApiOperation(value="货物名称信息-根据引领型和提送货查询货物名称列表", notes="货物名称信息-根据引领型和提送货查询货物名称列表")
	@GetMapping(value = "/m/getGoods")
	public Result<?> mGetGoods(@RequestParam(name="leadingEnterprise") String leadingEnterprise,
							   @RequestParam(name = "transportStatus") String transportStatus) {
		LambdaQueryWrapper<BasicGoods> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(BasicGoods::getLeadingEnterprise, leadingEnterprise);
		wrapper.eq(BasicGoods::getTransportStatus, transportStatus);
		List<BasicGoods> list = basicGoodsService.list(wrapper);
		List<String> collect = list.stream().map(BasicGoods::getGoods).collect(Collectors.toList());
		return Result.OK(collect);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param basicGoods
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasicGoods basicGoods) {
        return super.exportXls(request, basicGoods, BasicGoods.class, "货物名称信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasicGoods.class);
    }

}
